#include <stdio.h>
#include <stdlib.h>
#include "../../../api/headers/dataTypes.h"
#include "../../../api/headers/diskIO.h"
#include "../../../api/headers/fat16.h"
#include "../../../api/headers/partitioning.h"
#include "../../../api/headers/errors.h"
#include "../../../api/headers/debugging.h"

#define IMAGE_PATH "c:\\OS\\flashDrive2.img"

int main()
{
	Partition tbl;
	s8int part;
	s8int buff[102400];
	BootBlock *boot;
	FileDescriptor resTable;
	char filePath[] = "a:/folder/juliet.txt";
	int res;
	int loaded;

	if(addDisk(IMAGE_PATH, 0) != 0)
	{
		printf("%s", getLastErrorMessage());
		closeAllFiles();
		getchar();
		return 1;
	}

	part = mount(0, 0);

	if(getLastError() != NO_ERROR)
	{
		printf("%s", getLastErrorMessage());
		closeAllFiles();
		getchar();
		return 1;
	}

	boot = &(getMountedPartitionInfo(part)->bootBlock);

	printf("Description: %s\nBytes per block: %d\nBlocks per AllocUnit: %d\nReserved blocks: %d\n"
			"Number of file alloc tables: %d\nNumber of root dir entries: %d\nTotal blocks: %d\n"
			"Media descriptor: %d\nBlocks per alloc table: %d\nVolume serialNumber: %d\n"
			"Label: %s\nFiles system id: %s\nRootDirStart: 0x%X\nRootDirSize: 0x%X\n",
			boot->description, boot->bytesPerBlock, boot->blocksPerAllocUnit, boot->reservedBlocks,
			boot->numberOfFats, boot->rootDirEntriesN, boot->totalBlocks, boot->mediaDescriptor,
			boot->blocksPerFat, boot->volumeSerialNumber, boot->volumeLabel, boot->fileSystemId,
			boot->rootDirStartSector, boot->rootDirSize
		);

	
	if(getLastError() != NO_ERROR)
	{
		printf("%s", getLastErrorMessage());
		closeAllFiles();
		getchar();
		return 1;
	}

	 
	// resTable.size = 0;
	 res = getDirectoryFromPath(filePath, &resTable, true);

	 if(getLastError() != NO_ERROR)
		printf("%s\n\n", getLastErrorMessage());
	 else
	 {

		 printf("\n\nResult dir: 0x%X\n\n", res);


		 printf("Name: %s\nExtension: %s\nIs directory: %d\n"
				"Is hidden: %d\nIs read only: %d\nIs system: %d\n"
				"Starting cluster: 0x%X\nFile size (Bytes): %d\n\n",
				resTable.name, resTable.extension, resTable.isDirectory,
				resTable.isHidden, resTable.isReadOnly, resTable.isSystem,
				resTable.startingCluster, resTable.size
				);
	 }

	//printf("[0x9C]: %x\n", buff[0x9c]);

	//printDump(buff, 512);

	getFileList(filePath, buff);
	if(getLastError() != NO_ERROR)
		printf("%s\n\n", getLastErrorMessage());
	else
		printf("Files: \n%s\n\n", buff);


	loaded = loadFile(&resTable, (u8int*)buff);

	if(getLastError() != NO_ERROR)
		printf("Loading failed: %s\n\n", getLastErrorMessage());
	else
	{
		printf("Loaded (Bytes): %d\n\n", loaded);
		buff[loaded] = 0;
		printf("File content: \n\n%s", buff);

	}

	//printf("Bootable: %X\nStarting CHS: %X\nEnding CHS: %X\nPartition type: %X\nStarting sector: %X\nSize: %X", tbl.bootIndicator, tbl.startingCHS, tbl.endingCHS, tbl.type, tbl.startingSector, tbl.size);
	closeAllFiles();
	getchar();

	return 0;
}